package cn.anjubao.cp.api.config.security;

import cn.anjubao.cp.api.common.ErrorNum;
import cn.anjubao.cp.common.base.BaseResponse;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class AuthEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        BaseResponse<String> br = new BaseResponse<>();
        br.setResult(ErrorNum.NOT_AUTHENTICATED.code);
        br.setMessage(ErrorNum.NOT_AUTHENTICATED.message);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().println(JSON.toJSONString(br));
    }
}
